
/**
*
* @ author: hxwy
* @ email: 928936715@qq.com
* @ date: 2020-04-13 16:51
*/
export default class SpawnPlatform extends Laya.Script {

    initPos: number = -1;
    spawnCount : number = 0;
    prefab:Laya.Sprite3D;
    parent:Laya.Sprite3D;
    platforms : Array<Laya.Sprite3D> = new Array();
    constructor() {
        super();
    }
    init(prefab,parent)
    {
        this.prefab = prefab;
        this.parent = parent;

        for (let i = 0; i < 5; i++) {
            this.Spawn();         
        }
        Laya.stage.on("CreatePlatform",this,this.Spawn);
        Laya.stage.on("ContinueGame",this,function()
        {
            this.platforms.forEach(element => {
                let item = (element as Laya.Sprite3D);
                if(item.displayedInStage)
                {
                    item.removeSelf();
                    //item.destroy();
                    Laya.Pool.recover("Platform",item);
                }
            });
            this.platforms = new Array();
            this.spawnCount = 0;
            for (let i = 0; i < 5; i++) {
                this.Spawn();         
            }

        });  
    }
    Spawn()
    {
        let temp = Laya.Pool.getItemByCreateFun("Platform", this.CreateFun, this) as Laya.MeshSprite3D;
        this.platforms.push(temp);
        this.parent.addChild(temp);
        temp.transform.localPositionY = this.initPos - (this.spawnCount * 1.5);
        this.setPlatform(temp);
        this.spawnCount++;
    }
    CreateFun()
    {
        return Laya.Sprite3D.instantiate(this.prefab,this.parent);
    }
    setPlatform(platform)
    {
        let pf = platform as Laya.Sprite3D;

        if(this.spawnCount == 0)
        {
            for (let i = 0; i < pf.numChildren; i++) {
                let child = pf.getChildAt(i) as Laya.MeshSprite3D; 
               
                
                (child.meshRenderer.material as Laya.PBRStandardMaterial).albedoColor = new Laya.Vector4(0,0,0,1);
                child.name = "Bar";
                if(i == 0 || i == 1)
                {
                    child.meshRenderer.enable = false;
                    child.getComponent(Laya.PhysicsCollider).isTrigger = true;
                }
                else
                {
                    child.meshRenderer.enable = true;
                    child.getComponent(Laya.PhysicsCollider).isTrigger = false;
                }
            }
            return;
        }

        //障碍物
        let obstacles = this.getRandom(0, pf.numChildren - 1);

        let nullCount = 0;
        for (let i = 0; i < pf.numChildren; i++) 
        {
            let child = pf.getChildAt(i) as Laya.MeshSprite3D; 
            child.meshRenderer.enable = true;
            child.getComponent(Laya.PhysicsCollider).isTrigger = false;
            (child.meshRenderer.material as Laya.PBRStandardMaterial).albedoColor = new Laya.Vector4(0,0,0,1);
            child.name = "Bar";

            if(nullCount <= 4 && this.getRandom(0, 10) > 8)
            {
                child.meshRenderer.enable = false;
                child.getComponent(Laya.PhysicsCollider).isTrigger = true;
                nullCount++;
            }
            else if(obstacles == i)
            {
                child.name = "obstacles";
                //需要强转
                (child.meshRenderer.material as Laya.PBRStandardMaterial).albedoColor = new Laya.Vector4(1,0,0,1);
            }
        }
        if(nullCount == 0)
        {
            let child = pf.getChildAt(0) as Laya.MeshSprite3D; 
            child.name = "Bar";
            child.meshRenderer.enable = false;
            child.getComponent(Laya.PhysicsCollider).isTrigger = true;
        }
    }

    getRandom(min,max)
    {
        let delta = max - min;
        let ranValue = Math.random() * delta;
        ranValue = Math.round(ranValue);
        return min + ranValue;
    }
}
